package com.vpclub.dao.upgrade;

import com.vpclub.bean.Customer;
import com.vpclub.dao.CustomerDao;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

/**
 * @author : leimingchao
 * @version V1.0
 * @Project: jdbc
 * @Package com.vpclub.dao.impl
 * @Description: TODO
 * @date Date : 2021年05月30日 2:14
 */
public class CustomerDaoImpl extends BaseDao<Customer> implements CustomerDao {

    @Override
    public void insert(Connection connection, Customer customer) {
        String sql = "insert into customers (name,email,birth) values (?,?,?) ";
        Object[] args = {customer.getName(), customer.getEmail(), customer.getBirth()};
        update(connection, sql, args);
    }

    @Override
    public void deleteById(Connection connection, Integer id) {
        String sql = "delete from customers where id = ? ";
        update(connection, sql, id);
    }

    @Override
    public void update(Connection connection, Customer customer) {
        String sql = "update customers set name = ? , email = ? , birth = ? where id = ? ";
        Object[] args = {customer.getName(), customer.getEmail(), customer.getBirth(), customer.getId()};
        update(connection, sql, args);
    }

    @Override
    public Customer getCustomerById(Connection connection, Integer id) {
        String sql = "select id, name, email, birth from customers where id =? ";
        return getInstance(connection, sql, id);
    }

    @Override
    public List<Customer> getAll(Connection connection) {
        String sql = "select id, name, email, birth from customers ";
        return getForList(connection, sql);
    }

    @Override
    public Long getCount(Connection connection) {
        String sql = "select count(*) from customers ";
        return getValue(connection, sql);
    }

    @Override
    public Date getMaxBirth(Connection connection) {
        String sql = "select max(birth) from customers ";
        return getValue(connection, sql);
    }
}
